<?php
namespace Admin\Controller;
use Lib\Api\OrganizationApi;
use Think\Controller;
class AdminController extends Controller {

    public function index(){
        //session('user', 'Mr.Lee');
        //session('member', 'o1k');
        //session(null);
        //echo session('user');
        //echo session('member');
        //echo session('?name');

        $pageName = 'login1';
        if (empty(session('?admin_name'))) {
            $this->display($pageName);
        } else {
            $manager_id = session('admin_id');
            $manager_name = session('admin_name');
            $menu_list = getMenuByAdmin($manager_id, $manager_name);
            $this->assign('data', $menu_list);
            $this->display();
        }
        //$this->display();
    }

    public function testaabb() {
        $managerModel = M('Manager');
        $managerObj = $managerModel->select();
        session('admin_id', $managerObj[0]['id']);
        session('admin_name', $managerObj[0]['name']);
        session('admin_real_name', $managerObj[0]['real_name']);
        $this->display();
    }

    public function login() {
        $map['b.name'] = $_POST['name'];
        $map['b.password'] = md5($_POST['password']);
        $map['b.state'] = 1;
        $managerObj = OrganizationApi::getInfo($map, "a.id, a.level, a.code, a.p_code, a.name, b.head, b.id as customer_id");
        if (empty($managerObj)) {
            $data['code'] = -1;
            $data['msg'] = '账号或密码不正确，请重试';
        } else {
            $data['code'] = 0;
            $data['msg'] = '登录成功';

            //Session
            session('admin_id', $managerObj['id']);
            session('admin_name', $managerObj['name']);
            session('admin_head', $managerObj['head']);
            session('admin_code', $managerObj['code']);
            session('admin_p_code', $managerObj['p_code']);
            session('admin_level', $managerObj['level']);
            session('customer_id', $managerObj['customer_id']);
        }
        $this->ajaxReturn($data);
    }

    public function loginout() {
        session('admin_id', null);
        session('admin_name', null);
        session('admin_real_name', null);
        $data['code'] = 0;
        $this->ajaxReturn($data);
    }
}